package com.atguigu.demojuc.chap02;

import java.util.concurrent.TimeUnit;
import java.util.concurrent.locks.ReentrantLock;

/**
 * 通过JUC中提供的ReentrantLock里的tryLock方法，限时等待，解决死锁问题。
 */
public class DeadlockDemo2 {
    private static ReentrantLock lock1 = new ReentrantLock();
    private static ReentrantLock lock2 = new ReentrantLock();

    public static void main(String[] args) {

        new Thread(() -> {
            try {
                boolean flag1 = lock1.tryLock(100, TimeUnit.MILLISECONDS);
                if (flag1) {
                    try {
                        System.out.println("A线程获取到lock1了");
                        /*try {
                            Thread.sleep(300);
                        } catch (InterruptedException e) {
                            throw new RuntimeException(e);
                        }*/
                        boolean flag2 = lock2.tryLock(100, TimeUnit.MILLISECONDS);
                        if (flag2) {
                            try {
                                System.out.println("A线程获取到lock2了");
                                System.out.println("A获取了lock1和lock2,开开心心干活...");
                            } finally {
                                System.out.println("A线程释放lock2了");
                                lock2.unlock();
                            }
                        } else {
                            System.out.println("A线程没有获取到lock2");
                        }
                    } finally {
                        System.out.println("A线程释放lock1了");
                        lock1.unlock();
                    }

                } else {
                    System.out.println("A线程没有获取到lock1");
                }
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }, "A").start();

        new Thread(() -> {
            try {
                boolean flag2 = lock2.tryLock(100, TimeUnit.MILLISECONDS);
                if (flag2) {
                    try {
                        System.out.println("B线程获取到lock2了");
                        /*try {
                            Thread.sleep(500);
                        } catch (InterruptedException e) {
                            throw new RuntimeException(e);
                        }*/
                        boolean flag1 = lock1.tryLock(100, TimeUnit.MILLISECONDS);
                        if (flag1) {
                            try {
                                System.out.println("B线程获取到lock1了");
                                System.out.println("B获取了lock2和lock1,开开心心干活...");
                            } finally {
                                System.out.println("B线程释放lock1了");
                                lock1.unlock();
                            }
                        } else {
                            System.out.println("B线程没有获取到lock1");
                        }
                    } finally {
                        System.out.println("B线程释放lock2了");
                        lock2.unlock();
                    }

                } else {
                    System.out.println("B线程没有获取到lock2");
                }
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }, "B").start();
    }
}
